@extends('layouts.layout')

@section('title', '权限列表')

@section('CONTENT')
    <div class="box">
        <div class="box-body">
        </div>
    </div>
    <div class="box">
        <div class="box-header with-border">
            <h3 class="box-title">权限列表</h3>
            <div class="box-tools">
                @can('create-permission')
                    <a href="{{ route('permission.create') }}" class="btn btn-default btn-sm">添加权限</a>
                @endcan
            </div>
        </div>
        <!-- /.box-header -->
        <div class="box-body">
            @if( isset($permissions) )
                <table class="table table-condensed">
                    <tr>
                        <th>权限标识</th>
                        <th>权限名</th>
                        <th style="width: 200px">操作</th>
                    </tr>
                    @foreach($permissions as $permission)

                        <tr {{ $permission['pid'] != 0 ? "class=hidden "."data-pid=".$permission['pid'] : '' }} level="{{ $permission['level'] }}"  >
                            <td>{{ $permission['cn_name'] }}</td>
                            <td>{{ $permission['name'] }}</td>
                            <td>
                                @can('delete-permission')
                                    <span class="meta pull-right">
                                            <form id="Delete-{{ $permission['name'] }}" action="{{ route('permission.destroy',['id'=>$permission['id']]) }}" method="post">
                                                {{ csrf_field() }}
                                                {{ method_field('DELETE') }}
                                                <button type="button" data-action="Delete-{{ $permission['name'] }}" action="DeleteAction" class="btn btn-default btn-sm pull-left">删除</button>
                                            </form>
                                        </span>
                                @endcan
                                @if($permission['level'] < 3 )
                                    <span class="meta pull-right">
                                        <button type="button" action="show-childs" data-id="{{ $permission['id'] }}" data-pid="{{ $permission['pid'] }}" class="btn btn-default btn-sm pull-left">查看子权限 <i class="fa fa-chevron-left"></i></button>
                                    </span>
                                @endif
                                @can('edit-permission')
                                    <span class="meta pull-right">
                                        <a href="{{ route('permission.edit',['id'=>$permission['id']]) }}" class="btn btn-default btn-sm pull-left">编辑</a>
                                    </span>
                                @endcan



                            </td>
                        </tr>

                    @endforeach
                </table>
            @else
                <h5>还没有权限哟，赶快去创建一个吧！</h5>
            @endif
        </div>
        <!-- /.box-body -->
        <div class="box-footer clearfix">
            {{--{{ $articles->links() }}--}}
        </div>
    </div>
@endsection

@section('Style')
    <link href="https://cdn.bootcss.com/layer/3.1.0/theme/default/layer.css" rel="stylesheet">
@endsection
@section('JavascriptResult')
    <script src="https://cdn.bootcss.com/layer/3.1.0/layer.js"></script>
@endsection

@section('JavascriptScript')
    @can('delete-permission')
        $('button[action="DeleteAction"]').on('click',function(){
            var action  = $(this).data('action')
            layer.confirm('确定要删除？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $("#"+action).submit();
            }, function(){
                layer.msg('好的👌', {
                    time: 2000,
                });
            });
        });
    @endcan

    $('button[action="show-childs"]').on('click',function(){
        var showPid = $(this).data('id');
        $('tr[data-pid="'+ showPid +'"]').toggleClass('hidden')
        if($(this).children('i').hasClass('fa-chevron-left')){
            $(this).children('i').addClass('fa-chevron-down').removeClass('fa-chevron-left');
        }else{
            $(this).children('i').addClass('fa-chevron-left').removeClass('fa-chevron-down');
            if($(this).data('pid') == 0 ){
                $('button[action="show-childs"]').filter('button[data-pid="'+ showPid +'"]').each(function(){
                    var bpid = $(this).data('pid');
                    if($(this).children('i').hasClass('fa-chevron-down')){
                        $(this).click();
                    }
                });
            }
        }
    });

    $('tr[level="2"]').addClass('info');
    $('tr[level="3"]').addClass('active');
@endsection